package cn.stylefeng.guns.czlm.service;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.czlm.entity.TbProduct;
import cn.stylefeng.guns.czlm.mapper.TbProductMapper;
import cn.stylefeng.guns.modular.system.entity.Student;
import cn.stylefeng.guns.modular.system.model.StudentDto;
import cn.stylefeng.roses.core.util.HttpContext;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class TbProductService extends ServiceImpl<TbProductMapper, TbProduct> {

    @Resource
    private TbProductMapper tbProductMapper;


    public Page<Map<String, Object>> listProductByMap(TbProduct tbProduct){
        //通过layui获得分页信息
        Page page = LayuiPageFactory.defaultPage();
        Page<Map<String, Object>> page1=tbProductMapper.listProductByMap(page,tbProduct);
        HttpServletRequest request = HttpContext.getRequest();
     Integer pageIndex=Integer.parseInt(request.getParameter("page"));
        if(pageIndex>page1.getPages()){//以下重写了 LayuiPageFactory.defaultPage();

            //每页多少条数据
            int limit = Integer.valueOf(request.getParameter("limit"));
            page=   new Page(1, limit);
            return tbProductMapper.listProductByMap(page,tbProduct);
        }
        return page1;
    }

    public boolean delProduct(Integer id){ return tbProductMapper.deleteById(id)>1?true:false; }//删除
    public boolean ModifyIsMarketable(TbProduct tbProduct){ return tbProductMapper.ModifyIsMarketable(tbProduct); }//上下架
    public boolean ModifyIsRecommend(TbProduct tbProduct){ return tbProductMapper.ModifyIsRecommend(tbProduct); }//首推
  public   TbProduct tbProductById(Integer id){ return tbProductMapper.tbProductById(id); }//id查询
    public List<String> listThumbnail(){ return tbProductMapper.listThumbnail(); }//查询全部
    public void  ModifySave(TbProduct tbProduct){//保存
        tbProductMapper.ModifySave(tbProduct);
    }//修改

    public void insertTbProduct(TbProduct tbProduct) {
      tbProductMapper.insertTbProduct(tbProduct);
    }
}
